require "./mdx"
require "./mdl"
require "./txt"

file = ""
target = ""
parser = nil

begin
	if ARGV.size < 1
		raise "Not enough command-line arguments"
	end
	
	file = ARGV[0]
	
	if ARGV.size > 1
		target = ARGV[1]
	else
		target = File.basename(file, File.extname(file)) + ".txt"
	end
	
	case File.extname(file).downcase
		when ".mdx" then parser = MDX.new(file)
		when ".mdl" then parser = MDL.new(file)
		when ".txt" then parser = TXT.new(file)
		else raise "Unsupported extension \"#{File.extname(file).downcase}\" (supported: .mdx .mdl .txt)"
		end
		
	parser.info

	case File.extname(target).downcase
		when ".mdx" then parser.saveMDX(target)
		when ".mdl" then parser.saveMDL(target)
		when ".txt" then parser.saveTXT(target)
		else raise "Unsupported extension \"#{File.extname(target).downcase}\" (supported: .mdx .mdl .txt)"
	end
rescue Exception => e
	puts "Exception: #{e.message}"  
	puts "Backtrace: #{e.backtrace.inspect}"
end